﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace FanYi
{
    public static class Config
    {
        private static readonly Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

        /// <summary>
        /// 筛选扩展名
        /// </summary>
        public static string FileExtension
        {
            get
            {
                return ConfigurationManager.AppSettings[ConfigKeys.FileExtension];
            }
            set
            {
                Update(ConfigKeys.FileExtension, value);
            }
        }

        /// <summary>
        /// 百度翻译应用AppID
        /// </summary>
        public static string AppId
        {
            get
            {
                return ConfigurationManager.AppSettings[ConfigKeys.AppId];
            }
            set
            {
                Update(ConfigKeys.AppId, value);
            }
        }

        /// <summary>
        /// 百度翻译应用密钥
        /// </summary>
        public static string Key
        {
            get
            {
                return ConfigurationManager.AppSettings[ConfigKeys.Key];
            }
            set
            {
                Update(ConfigKeys.Key, value);
            }
        }

        /// <summary>
        /// 翻译源语言
        /// </summary>
        public static string From
        {
            get
            {
                return ConfigurationManager.AppSettings[ConfigKeys.From];
            }
            set
            {
                Update(ConfigKeys.From, value);
            }
        }

        /// <summary>
        /// 译文语言
        /// </summary>
        public static string To
        {
            get
            {
                return ConfigurationManager.AppSettings[ConfigKeys.To];
            }
            set
            {
                Update(ConfigKeys.To, value);
            }
        }

        /// <summary>
        /// 翻译应用延时
        /// </summary>
        public static int Delay
        {
            get
            {
                int.TryParse(ConfigurationManager.AppSettings[ConfigKeys.Delay], out int value);
                return value;
            }
            set
            {
                Update(ConfigKeys.Delay, value.ToString());
            }
        }

        /// <summary>
        /// 当前选择的文件夹目录
        /// </summary>
        public static string CurrentPath
        {
            get
            {
                return ConfigurationManager.AppSettings[ConfigKeys.CurrentPath];
            }
            set
            {
                Update(ConfigKeys.CurrentPath, value);
            }
        }

        /// <summary>
        /// 当前任务索引
        /// </summary>
        public static int CurrentIndex
        {
            get
            {
                int.TryParse(ConfigurationManager.AppSettings[ConfigKeys.CurrentIndex], out int value);
                return value;
            }
            set
            {
                Update(ConfigKeys.CurrentIndex, value.ToString());
            }
        }

        /// <summary>
        /// 添加或更新配置信息
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        private static void Update(string key, string value)
        {
            var exist = config.AppSettings.Settings.AllKeys.Contains(key);
            if (exist)
            {
                config.AppSettings.Settings[key].Value = value;
            }
            else
            {
                config.AppSettings.Settings.Add(key, value);
            }
            config.Save();
        }
    }

    public class ConfigKeys
    {
        public const string FileExtension = "FileExtension";
        public const string AppId = "AppId";
        public const string Key = "Key";
        public const string From = "From";
        public const string To = "To";
        public const string Delay = "Delay";
        public const string CurrentPath = "CurrentPath";
        public const string CurrentIndex = "CurrentIndex";
    }

}
